import { Serializable } from "../../util/util";

export class PlanedStructure extends Serializable{
    /**
     * 
     * @param {RoomPosition|{x:Number,y:Number,roomName:String}} pos 
     * @param {BuildableStructureConstant|} structureType 
     */
    constructor(pos, structureType){
        super();
        this.pos = new RoomPosition(pos.x,pos.y,pos.roomName);
        this.structureType = structureType;
        this.structureId = undefined;
    }
    get x(){
        return this.pos.x;
    }
    get y(){
        return this.pos.y;
    }
    get roomName(){
        return this.pos.roomName;
    }
    serialize(){
        return {
            pos: this.pos,
            type: this.structureType,
            id: this.structureId,
        };
    }
    deserialize(obj){
        this.pos = obj.pos;
        this.structureType = obj.type;
        this.structureId = obj.id;
        return this;
    }
    isPlanned(){
        let room = Game.rooms[this.pos.roomName];
        if(!room){
            return undefined; //construction sites will not bring vision
        }
        
        let looks = room.lookForAt(this.structureType, this.pos);
        for(let look of looks){
            if(look instanceof Creep){
                return look.memory.role === "structure";
            }
            return true;
        }
        looks = room.lookForAt(LOOK_CONSTRUCTION_SITES, this.pos);
        for(let look of looks){
            if(look.structureType === this.structureType){
                return true;
            }
        }
        return false;
    }
}

export class StructurePlan extends Serializable{
    /**
     * 
     * @param {Room} spawnRoom 
     */
    constructor(spawnRoom = undefined){
        if(spawnRoom !== undefined){
            this.spawnRoom = spawnRoom;
            this.planList = []
        }
    }

    serialize(){//???
        let list = [];
        for(let plan of planList){
            
        }
        return {
            super: super.serialize(),
            spawnRoomName: this.spawnRoomName,
            // list: 
        };
    }
    deserialize(obj){
        this.spawnRoomName = obj.spawnRoomName;
        return this;
    }

    get spawnRoom(){
        if(this._spawnRoom !== undefined){
            return this._spawnRoom;
        }

        let spawnRoom = Game.rooms[this.spawnRoomName];
        if(!spawnRoom){
            return this._spawnRoom = null;
        }
        return this._spawnRoom = spawnRoom;
    }

    /**
     * 
     * @param {Room} spawnRoom 
     */
    set spawnRoom(spawnRoom){
        if(!spawnRoom){
            this.spawnRoomName = undefined;
            return this._spawnRoom = null;
        }
        this.spawnRoomName = spawnRoom.name;
        return this._spawnRoom = spawnRoom;
    }

}